package hust.music.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import hust.music.dao.JpaDao;
import hust.music.entity.Comment;
import hust.music.entity.File;
import hust.music.entity.Label;
import hust.music.entity.Message;
import hust.music.entity.Music;
import hust.music.entity.User;
import hust.music.service.MusicModService;
import hust.music.util.DateConvert;
import hust.music.util.DynamicInfo;

/**
 * MusicModServiceImpl
 * 
 * @author Administrator
 * @since 1.0
 */
@Service("musicModService")
@Transactional
public class MusicModServiceImpl implements MusicModService {

	@Autowired
	private JpaDao jpaDao;

	/**
	 * 上传音乐
	 * 
	 * @param music 音乐
	 * @param user 用户
	 * @param file 文件
	 * @param styles 流派
	 * @return true
	 */
	@Override
	public boolean uploadMusic(Music music, User user, File file,
			String[] styles) {
		User u = jpaDao.get(User.class, user.getId());
		File f = jpaDao.get(File.class, file.getId());

		music.setUser(u);
		music.setFile(f);

		for (String style : styles) {
			List<Label> list = jpaDao.findAll(Label.class,
					"select l from Label l where l.title = ?", style);
			Label label = null;
			if (list.size() == 0) {
				label = new Label();
				label.setTitle(style);
			} else {
				label = list.get(0);
			}
			music.getLabels().add(label);
		}

		jpaDao.save(music);

		return true;
	}

	@Override
	public Music findMusic(Integer id) {
		// TODO Auto-generated method stub
		List<Music> list = jpaDao.findAll(Music.class,
				"from Music where id = ?1", id);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/** 按时间 */
	@Override
	public List<Music> display_time(Music music) {
		// TODO Auto-generated method stub
		// 设定时间
		Calendar c = Calendar.getInstance();
		c.set(2012, 12, 24, 00, 00, 00);
		@SuppressWarnings("unused")
		Date date = c.getTime();
		List<Music> list = jpaDao.findAll(Music.class,
				"from Music m where m.uploadTime < date");

		return list;

	}

	/** 按收听量 */
	@Override
	public List<Music> display_listenTimes(Music music) {
		// TODO Auto-generated method stub
		List<Music> list = jpaDao.findAll(Music.class,
				"from Music m order by m.listenTimes desc");
		return list;
	}

	/** 按评分 */
	public List<Music> display_mark(Music music) {
		List<Music> list = jpaDao.findAll(Music.class,
				"from Music m order by m.mark desc");
		return list;
	}

	/** 标签返回列表 */
	@Override
	public List<Music> display_label(Label label) {
		// TODO Auto-generated method stub
		List<Music> list = jpaDao
				.findAll(
						Music.class,
						"from Music"
								+ ("select m from Music m,Lbael l where l.id = ? and m in elements(l.musics)"));

		return list;
	}

	/** 返回单首歌曲信息 */
	@Override
	public Music music_item1(int id) {
		// TODO Auto-generated method stub

		List<Music> list = jpaDao.findAll(Music.class, "from Music"
				+ "select m from Music m where m.id = id");

		return list.get(0);

	}

	/** 返回单首歌曲的评论 */
	@Override
	public List<DynamicInfo> getComments(int id, int firstResult, int maxResult) {
		// TODO Auto-generated method stub
		List<Comment> list = jpaDao
				.findByPage(
						Comment.class,
						"select c from Comment c where c.music = ? order by c.time desc",
						jpaDao.get(Music.class, id), firstResult, maxResult);
		List<DynamicInfo> comments = new ArrayList<DynamicInfo>();
		for (Comment c : list) {
			DynamicInfo dynamic = new DynamicInfo(c.getUser().getId(), c
					.getUser().getNickname(), c.getUser().getAvatar(), null,
					null, null, null, null, c.getContent(), DateConvert
							.convertDateToString(c.getTime()), null, null);
			comments.add(dynamic);
		}
		return comments;
	}

	@Override
	public int getTotalComments(int id) {
		// TODO Auto-generated method stub
		return jpaDao
				.findAll(
						Comment.class,
						"select c from Comment c where c.music = ? order by c.time desc",
						jpaDao.get(Music.class, id)).size();
	}

	@Override
	public boolean addComment(Comment c, int id, User user) {
		// TODO Auto-generated method stub
		try {
			Music music = jpaDao.get(Music.class, id);
			User u = jpaDao.get(User.class, user.getId());
			c.setUser(u);
			c.setMusic(music);
			jpaDao.save(c);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public int addListen(int id) {
		Music music = jpaDao.get(Music.class, id);
		int listen = music.getListenTimes();
		System.out.println(listen);
		listen++;
		System.out.println(listen);
		music.setListenTimes(listen);

		return listen;
	}

	@Override
	public int mark(int id, int mark) {
		Music music = jpaDao.get(Music.class, id);
		double oldMark = music.getMark();
		int size = music.getSize();
		double newMark = (oldMark * size + mark) / (size + 1);
		music.setMark(newMark);
		music.setSize(size + 1);

		if (newMark < 3) {
			return 2;
		} else if (newMark < 5) {
			return 4;
		} else if (newMark < 7) {
			return 6;
		} else if (newMark < 9) {
			return 8;
		}
		return 10;
	}

	@Override
	public boolean addMessage(Message m, User u, Integer uid) {
		// TODO Auto-generated method stub
		User user = jpaDao.get(User.class, u.getId());
		User user2 = jpaDao.get(User.class,uid);
		m.setInitiativeUser(user);
		m.setPassiveUser(user2);
		jpaDao.save(m);
		return true;
	}

	@Override
	public User getUploader(Integer id) {
		// TODO Auto-generated method stub
		Music music = jpaDao.get(Music.class, id);
		User user = new User();
		user.setId(music.getUser().getId());
		user.setNickname(music.getUser().getNickname());
		return user;
	}

}
